package concurrency.chapter1;

import jdk.dynalink.beans.StaticClass;

public class TryConcurrency {
    public static void main(String[] args) {
        //匿名内部类的方式创建线程，创建Thread的子类，重写run方法
        new Thread("Read-Thead") {
            @Override
            public void run() {
                println(Thread.currentThread().getName());
                readFromDataBase();
            }
        }.start();
        //给Thread类传入Runnable接口
        new Thread(new Runnable() {
            @Override
            public void run() {
                writeDataToFile();
            }
        }, "Write-Thead").start();
    }

    private static void readFromDataBase() {
        try {
            println("Begin read data from db.");
            Thread.sleep(1000 * 10L);
            println("Read data done and start handle it.");
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        println("The data handle finish and successfully.");
    }

    private static void writeDataToFile() {
        try {
            println("Begin write data to file.");
            Thread.sleep(2000 * 10L);
            println("write data done and start handle it.");
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        println("The data handle finish and successfully.");
    }

    private static void println(String message) {
        System.out.println(message);
    }
}
